<?php
#$this->setDataAndRender('user', 'User', array('with'=>'group', 'together'=>true));

class DefaultController extends AdminController {
    
    private function setDataAndRender($viewToRender, $modelClass) {
        $dataProvider = new CActiveDataProvider($modelClass, array(
            'pagination'=>array('pageSize'=>20),
        ));
        $this->render($viewToRender, array('dataProvider' => $dataProvider));
    }
    
    public function actionIndex() {
        $this->render('index');
    }
    
    public function actionLogin() {
        $model = new Login();
        if (isset($_POST['Login'])) {
            $model->attributes = $_POST['Login'];
            if($model->loginUser()) {
                $this->redirect(array('default/index'));
            }
        }
        $this->layout = 'empty';
        $this->render('login', array('model'=>$model));
    }
    
    public function actionUser() {
        $totalItemCount = Yii::app()->db->createCommand('SELECT count(*) from `users`')->queryScalar();
        $this->render('user', array('totalItemCount'=>$totalItemCount));
    }
    
    public function actionGroup() {
        $this->setDataAndRender('group', 'Group');
    }
    
    public function actionPartnerCat() {
        $dataDP = PartnerCat::model()->with('parent')->findAll();
        foreach (!empty($dataDP) ? $dataDP : array() as $item) 
            $data[] = array('name'=>$item->name, 'parent'=>$item->parent['name'], 'id'=>$item->catId);
        $dataProvider = new CArrayDataProvider(isset($data) ? $data : array(), array(
            'sort'=>array('attributes'=> array('login', 'email', 'group'))
        ));
        $this->render('partnerCat', array('dataProvider'=>$dataProvider));
    }
    
    public function actionNews() {
        $this->setDataAndRender('news', 'News');
    }
    
    public function actionPartner() {
        $partners = Yii::app()->db->createCommand()->select('p.name, p.partnerid as id, c.catId, c.name as catName')->from('partners p')->join('partners_cat c', 'c.catId=p.catId')->queryAll();
        if(!empty($partners)) {
            $idArr = array();
            foreach ($partners as $p) $idArr[] = $p['id'];
            $subCats = Yii::app()->db->createCommand()->select('c.name, c.catId, r.partnerId')->from('partners_sub_cat_rel r')->join('partners_cat c', 'c.catId=r.catId')->where(array('in', 'r.partnerId', $idArr))->queryAll();
            $count = count($partners);
            for($i=0; $i < $count; $i++) {
                $partners[$i]['subCats'] = '';
                for($l=0; $l<count($subCats); $l++) {
                    if($partners[$i]['id'] == $subCats[$l]['partnerId']) 
                        $partners[$i]['subCats'] .= !empty($partners[$i]['subCats']) ? ','.$subCats[$l]['name'] : $subCats[$l]['name'];
                }
            }
        }
        $dataProvider = new CArrayDataProvider($partners, array('sort'=>array('attributes'=> array('name', 'catName', 'subCats')) ));
        $this->render('partner', array('dataProvider'=>$dataProvider));
    }
    
    public function actionRedactor() {
        if(isset($_POST['work']) && !isset($_POST['new'])) $dataDP = Reviews::model()->with(array('user'=>array('select'=>'login'), 'partner.cat.subcat'))->findAll('status=:work', array(':work'=>'В работе'));
        else if(isset($_POST['new']) && !isset($_POST['work'])) $dataDP = Reviews::model()->with(array('user'=>array('select'=>'login'), 'partner.cat.subcat'))->findAll('status=:new', array(':new'=>'Новый'));
        else if(isset($_POST['new']) && isset($_POST['work'])) $dataDP = Reviews::model()->with(array('user'=>array('select'=>'login'), 'partner.cat.subcat'))->findAll('status=:work OR status=:new', array(':new'=>'Новый', ':work'=>'В работе'));
        else $dataDP = Reviews::model()->with(array('user'=>array('select'=>'login'), 'partner.cat.subcat'))->findAll();
        
        foreach ($dataDP ? $dataDP : array() as $item) {
            $diff = time() - $item->date;
            $date = sprintf('%02dд : %02dчас : %02dмин : %02dсек', $diff / 86400, ($diff % 86400) / 3600 , ($diff % 3600) / 60 , $diff % 60 );
            $data[] = array('userIp'=>$item['userIp'], 'id'=>$item->revId, 'date'=>$date, 'rating'=>$item->averageRating, 'partner'=>$item->partner['name'], 'cat'=>$item->partner['cat']['name'], 'subcat'=>isset($item->partner['cat']['subcat']['name']) ? $item->partner['cat']['subcat']['name'] : '', 'status'=>$item->status);
        }
        $dataProvider = new CArrayDataProvider(isset($data) ? $data : array(), array(
            'pagination'=>array('pageSize'=>20),
            'sort'=>array('attributes'=> array('date', 'rating', 'cat', 'subcat', 'status', 'userIp'))
        ));
        $this->render('redactor', array('dataProvider'=>$dataProvider));
    }
    
    public function actionLogoMenu() {
        $this->setDataAndRender('logoMenu', 'LogoMenu');
    }
    
    public function actionMainMenu() {
        $this->setDataAndRender('mainMenu', 'MainMenu');
    }
    
    public function actionPage() {
        $totalItemCount = Yii::app()->db->createCommand('SELECT count(*) from `pages`')->queryScalar();
        $this->render('page', array('totalItemCount'=>$totalItemCount));
    }
    
    public function actionOptions() {
        $this->setDataAndRender('option', 'Options');
    }
    
    public function actionMailing() {
        $this->setDataAndRender('mailing', 'Mailing');
    }
    
    public function actionBlogCat() {
        $this->setDataAndRender('blogCat', 'BlogCats');
    }
    
    public function actionBlogArt() {
        $dataProvider = new CArrayDataProvider(Yii::app()->db->createCommand()->select('a.name, a.url, a.artId as id, c.name as cat')->from('blog_arts a')->join('blog_cats c', 'c.catId=a.catId')->queryAll(), array(
            'sort'=>array('attributes'=> array('name', 'cat', 'url'))
        ));
        $this->render('blogArt', array('dataProvider'=>$dataProvider));
    }
    
    public function actionBanIp() {
        $totalItemCount = Yii::app()->db->createCommand('SELECT count(*) from `banned_ip`')->queryScalar();
        $this->render('banIp', array('totalItemCount'=>$totalItemCount));
    }
    
    public function actionPartErr() {
        $dataProvider = new CArrayDataProvider(Yii::app()->db->createCommand()->select('e.login, e.email, p.name, e.id')->from('error_reporting e')->join('partners p', 'p.partnerid=e.partnerId')->queryAll(), 
                array('sort'=>array('attributes'=> array('login', 'email', 'name'))));
        $this->render('partErr', array('dataProvider'=>$dataProvider));
    }
    
    public function actionKarmaParams() {
        $dataProvider = new CArrayDataProvider(Yii::app()->db->createCommand()->select('*')->from('karma_params')->queryAll(), 
                array('sort'=>array('attributes'=> array('timeDays', 'mesCount', 'partnersCount', 'revModer', 'mesDay_1_5', 'mesDay_3', 'mesDay_6', 'mesDayMore_6', 'comDay_1_5', 'comDay_3', 'comDay_6', 'comDayMore_6'))));
        $this->render('karmaParams', array('dataProvider'=>$dataProvider));
    }
    
    public function actionBanUser() {
        $totalItemCount = Yii::app()->db->createCommand('SELECT count(*) from `users` WHERE unblockDate>'.time().' or isBanned>0')->queryScalar();
        $this->render('banUser', array('totalItemCount'=>$totalItemCount));
    }
    
    public function actionBrending() {
        $dataProvider = new CArrayDataProvider(Yii::app()->db->createCommand()->select('b.id, b.type, b.url, b.butImg, c.name as catName, p.name')->from('brending b')->leftJoin('partners p', 'p.partnerid=b.typeId and b.type="partner"')->leftJoin('partners_cat c', 'c.catId=b.typeId and b.type="cat"')->queryAll(),
                array('sort'=>array('attributes'=>array('type', 'catName', 'name', 'butImg', 'url'))));
        $this->render('brending', array('dataProvider'=>$dataProvider));
    }
    
    public function actionSponsorAdt() {        
        $bannersData = Yii::app()->db->createCommand()->select('b.code, b.type, b.bannerId as id')->from('banners b')->queryAll();
        if(!empty($bannersData)) {
            $idArr = array();
            for($i=0; $i<count($bannersData); $i++) $idArr[] = $bannersData[$i]['id'];
            $catsData = Yii::app()->db->createCommand()->select('c.name, br.bannerId')->from('banners_relations br')->join('partners_cat c', 'c.catId=br.catId')->where(array('in', 'bannerId', $idArr))->queryAll();
            for($i=0;$i<count($catsData);$i++) {
                for($j=0; $j<count($bannersData); $j++) {
                    if($bannersData[$j]['id'] == $catsData[$i]['bannerId']) {
                        if(!isset($bannersData[$j]['cats'])) $bannersData[$j]['cats'] = '';
                        $bannersData[$j]['cats'] .= empty($bannersData[$j]['cats']) ? $catsData[$i]['name'] : ', '.$catsData[$i]['name'];
                    }
                }
            }
        }
        $dataProvider = new CArrayDataProvider($bannersData, array('sort'=>array('attributes'=>array('code', 'type', 'cats'))));
        $this->render('sponsorAdt', array('dataProvider'=>$dataProvider));
    }
    
    public function actionBannersBig() {
        $bannersData = Yii::app()->db->createCommand()->select('type, id')->from('banners_big')->queryAll();
        $this->render('bannersBig', array('bannersData'=>$bannersData));
    }
}